package com.bmps.springcloud.insuredmicroservice.service.amnt.strategy;

import com.bmps.springcloud.insuredmicroservice.service.trial.strategy.TrialStrategy;
import com.bmps.springcloud.insuredmicroservice.service.trial.strategy.TrialStrategyFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 从配置文件中获取保费算保额策略
 * @author :Jacob
 * @date : 2020-06-07 17:00:00
 */
public class AmntStrategyFactory {
    private static Logger logger = LoggerFactory.getLogger(AmntStrategyFactory.class);

    private static ConcurrentHashMap<String, AmntStrategy> amntMap = new ConcurrentHashMap<>();

    static {
        try {
            Resource resource = new ClassPathResource("amntstrategy.properties");
            InputStream inputStream = resource.getInputStream();
            Properties properties = new Properties();
            properties.load(inputStream);
            for (String key : properties.stringPropertyNames()) {
                AmntStrategy amntStrategy = (AmntStrategy) Class.forName(properties.getProperty(key)).newInstance();
                Arrays.stream(key.split(",")).forEach(s -> {
                    amntMap.put(s, amntStrategy);
                    logger.info("add key: " + s + ", new instance:" + amntStrategy);
                });
            }
        } catch (IOException | IllegalAccessException | InstantiationException | ClassNotFoundException e) {
            logger.error("保费算保额策略异常，异常信息为：" + e.getMessage());
        }
    }
    public static AmntStrategy getAmntStrategy(String key) {
        return amntMap.get(key);
    }
}
